/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fait.ferm.ui;

import fait.ferm.global.Glob;
import fait.ferm.global.TB;
import fait.ferm.persists.Credit;
import fait.ferm.persists.Fine;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;

/**
 *
 * @author Greg
 */

public class CreditTabController  implements Initializable {
    /*  Таблица для вывода текущей информации по кредитам  */
    @FXML
    private TableView creditsTable;
    
    @FXML
    private BorderPane parent; //Окно-родитель для вывода диалогового окна
    
    @FXML
    private void newCreditClick() {
        showCreditTabDialog();
    }
    
    private void showCreditTabDialog() {
        try {
            BorderPane p = (BorderPane) FXMLLoader.load(getClass().getResource("CreditTabDialog.fxml"));
            p.managedProperty().set(true);
            
			Glob.dialogPane.centerProperty().set(p);
			Glob.dialogPane.toFront();
        } catch (IOException ex) {
            Logger.getLogger(CreditTabController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        ObservableList<TB.Table6Col> items = FXCollections.observableArrayList();
        ArrayList<Credit> al = (ArrayList<Credit>)Glob.getFermEngine().getCreditActive().obj;
        for(Credit cur_credit: al) {
            
            double amount_fine = 0.0;
            ArrayList<Fine> al_fine = (ArrayList<Fine>)Glob.getFermEngine().getFineFrom(cur_credit).obj;
            for(Fine cur_fine: al_fine) {
                
                amount_fine += cur_fine.amount_arrears;
                
            }
            items.add(new TB.Table6Col(cur_credit.date_beg.toString(), String.format("%.1f", cur_credit.amount_given), String.format("%.1f", cur_credit.amount_received), String.format("%.1f", (cur_credit.amount_given - cur_credit.amount_received)), String.format("%.1f", amount_fine), cur_credit.date_end.toString()));
            
        }
        
		TB.bind(creditsTable);
        creditsTable.setItems(items);

    }
    
}
